package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.GoodsOrderQuery;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConIntegralGoodsOrder;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.GeConIntegralGoodsOrderService;
import com.htgd.gardenexpo.vo.GoodsExchangeNumsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: 王秀田
 * @date: 2024/11/13 17:08
 * @description: 积分商品订单 前端控制器
 */
@RestController
@RequestMapping("/api/integralGoodsOrder")
@Api(tags = "积分商品订单")
@Slf4j
public class GeConIntegralGoodsOrderController {

    @Autowired
    private GeConIntegralGoodsOrderService geConIntegralGoodsOrderService;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConIntegralGoodsOrder>> list(GoodsOrderQuery goodsOrderQuery, HttpServletRequest request) {
        CommonResult<List<GeConIntegralGoodsOrder>> result = new CommonResult<>();
        try {
            PageInfo<GeConIntegralGoodsOrder> list = geConIntegralGoodsOrderService.pageList(goodsOrderQuery, request);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    @Transactional
    public CommonResult<GeConIntegralGoodsOrder> details(String id) {
        CommonResult<GeConIntegralGoodsOrder> result = new CommonResult<>();
        try {
            GeConIntegralGoodsOrder goodsOrder = geConIntegralGoodsOrderService.getById(id);
            if(goodsOrder.getPicture()!=null&&goodsOrder.getPicture()!=""){
                goodsOrder.setImages(JSONArray.parseArray(goodsOrder.getPicture(),String.class));
            }
            goodsOrder.setTypeText(geBaseDictService.getGoodsType(goodsOrder.getType()));
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(goodsOrder);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/getGoodsExchangeNums")
    @ApiOperation("获取积分商品兑换数")
    public CommonResult<GoodsExchangeNumsVO> getGoodsExchangeNums(@RequestParam(name = "pageNum",required = true,defaultValue = "1")Integer pageNum,
                                                                  @RequestParam(name = "pageSize",required = true,defaultValue = "10")Integer pageSize,
                                                                  @RequestParam(name = "orderBy",required = false,defaultValue = "DESC")String orderBy) {
        log.info("综管-获取积分商品兑换数：{},{},{}",pageNum,pageSize,orderBy);
        return  geConIntegralGoodsOrderService.getGoodsExchangeNums(pageNum,pageSize,orderBy);
    }
}
